//
//  XFOriginalView.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/16.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

#import "XFOriginalView.h"
#import "UIImageView+WebCache.h"
#import "XFPictureView.h"

@interface XFOriginalView ()
// 头像
@property(nonatomic, weak) UIImageView *iconView;
// 昵称
@property(nonatomic, weak) UILabel *nameLable;
// vip
@property(nonatomic, weak) UIImageView *vipView;
// 发布时间
@property(nonatomic, weak) UILabel *creatLable;
// 发布地点
@property(nonatomic, weak) UILabel *sourceLable;
// 正文
@property(nonatomic, weak) UILabel *textLable;
// 图片 view
@property(nonatomic, weak) XFPictureView *pictureView;

//约束条件
@property(nonatomic, strong) MASConstraint *bottomContrain;

@property(nonatomic, weak) UIView *sepView;
@end

@implementation XFOriginalView

- (void)setStatues:(XFStatues *)statues {
  _statues = statues;
  XFUser *user = statues.user;
  [self.iconView sd_setImageWithURL:user.headImageUrl
                   placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
  self.nameLable.text = statues.user.name;
  self.vipView.image = statues.user.mbrankImage;
  self.creatLable.text = @"11:11";   // statues.created_at;
  self.sourceLable.text = @"北京"; // statues.source;
  self.textLable.text = statues.text;

  //有图片
  [self.bottomContrain uninstall];
  if (statues.pictureUrl.count != 0) {
    self.pictureView.picUrls = statues.pictureUrl;
    self.pictureView.hidden = NO;
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
      self.bottomContrain = make.bottom.equalTo(self.pictureView.mas_bottom);
    }];
  } else {
    self.pictureView.hidden = YES;
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
      self.bottomContrain = make.bottom.equalTo(self.textLable.mas_bottom);
    }];
  }
}

// 1.重写 init 方法

- (instancetype)init {

  if (self = [super init]) {
    [self setupUI];
    [self setupFrame];
  }
  return self;
}

// 2. setupUI添加子控件
- (void)setupUI {
  //添加分割线
  UIView *sepView = [[UIView alloc] init];
  sepView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
  [self addSubview:sepView];
  self.sepView = sepView;

  UIImageView *iconView = [[UIImageView alloc]
      initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
  [self addSubview:iconView];
  self.iconView = iconView;

  UILabel *nameLable = [[UILabel alloc] initWithTitle:@"逗比"
                                                Color:[UIColor darkGrayColor]
                                                 Font:14
                                               Margin:0];
  [self addSubview:nameLable];
  self.nameLable = nameLable;

  UIImageView *vipView = [[UIImageView alloc] init];
  [self addSubview:vipView];
  self.vipView = vipView;

  UILabel *creatLable = [[UILabel alloc] initWithTitle:@"shijian"
                                                 Color:[UIColor orangeColor]
                                                  Font:10
                                                Margin:0];
  [self addSubview:creatLable];
  self.creatLable = creatLable;

  UILabel *sourceLable = [[UILabel alloc] initWithTitle:@"beijing"
                                                  Color:[UIColor orangeColor]
                                                   Font:10
                                                 Margin:0];
  [self addSubview:sourceLable];
  self.sourceLable = sourceLable;

  UILabel *textLable = [[UILabel alloc] initWithTitle:@"beijing"
                                                Color:[UIColor darkGrayColor]
                                                 Font:14
                                               Margin:kStatueCellMargin];
  [self addSubview:textLable];
  self.textLable = textLable;

  XFPictureView *pictureView = [[XFPictureView alloc] init];
  [self addSubview:pictureView];
  self.pictureView = pictureView;
}

// 3. setupFrame添加子控件约束
- (void)setupFrame {

  [self.sepView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.right.equalTo(self);
    make.height.equalTo(@kStatueCellMargin);
  }];

  [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.sepView.mas_bottom).offset(kStatueCellMargin);
    make.left.equalTo(self.mas_left).offset(kStatueCellMargin);
    make.width.equalTo(@kStatueCellWidth);
    make.height.equalTo(@kStatueCellWidth);
  }];

  [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.iconView);
    make.left.equalTo(self.iconView.mas_right).offset(kStatueCellMargin);
  }];

  [self.vipView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.iconView);
    make.left.equalTo(self.nameLable.mas_right).offset(kStatueCellMargin);
  }];

  [self.creatLable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.iconView.mas_right).offset(kStatueCellMargin);
    make.bottom.equalTo(self.iconView.mas_bottom);
  }];

  [self.sourceLable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.creatLable.mas_right).offset(kStatueCellMargin);
    make.bottom.equalTo(self.creatLable.mas_bottom);
  }];

  [self.textLable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.iconView.mas_left);
    make.top.equalTo(self.iconView.mas_bottom).offset(kStatueCellMargin);
  }];

  [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.textLable.mas_left);
    make.top.equalTo(self.textLable.mas_bottom).offset(kStatueCellMargin);
//    make.width.height.equalTo(@100);
  }];

  [self mas_makeConstraints:^(MASConstraintMaker *make) {
    self.bottomContrain = make.bottom.equalTo(self.pictureView.mas_bottom);
  }];
}

@end
